<template>
  <view>
    <!-- 搜索 -->
    <!-- 此时做固定定位的时候，需要在外层再包一个盒子，在此盒子的基础上来设置定位 -->
    <view class="search-box">
      <my-search @click="goToSearch"></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类的导航 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navHandler(item)">
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>
    <!-- 楼层导航 -->
    <view class="floor-list">
      <!-- 每一个楼层 -->
      <view v-for="(item,index) in floorList" :key="index">
        <!-- 标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 图片展示区域 -->
        <view class="floor-item-box">
          <!-- 左侧 -->
          <navigator class="left-img-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <!-- 右侧 -->
          <view class="right-img-box">
            <navigator v-for="(item2,index2) in item.product_list" :key="index2" v-if="index2 !== 0" :url="item2.url">
              <image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width + 'rpx'}"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import tarbarBarge from '../../mixins/tarbar-barge.js'
  export default {
    mixins: [tarbarBarge],
    data() {
      return {
        swiperList: [], // 轮播图
        navList: [], // 导航栏
        floorList: []
      };
    },
    // 小程序页面初始化的生命周期
    onLoad() {
      // 发送请求加载轮播图
      this.getSwiperList()
      this.getNavList()
      // 获取导航栏
      this.getFloorList()

    },
    methods: {
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        console.log(res.meta)
        if (res.meta && res.meta.status !== 200) return uni.$showMessage()
        // 将以下方法封装成全局的方法，然后在后续中都可以直接调用
        //   uni.showToast({
        //     title:"数据请求失败",
        //     duration:1500,
        //     icon:'none'
        //   })
        // }
        // 将请求的数据赋值给具体的值
        this.swiperList = res.message
        uni.$showMessage('数据加载成功')
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        console.log(res)
        if (res.meta.status !== 200) return uni.$showMessage()
        this.navList = res.message
      },
      navHandler(item) {
        console.log(item);
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$showMessage()
        // 修改正确的路径
        // res.message.url = '/subpkg/goods_list/goods_list' + res.message.navigator_url.split('?')[1]
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      goToSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      height: 100%;
      width: 100%;
    }
  }

  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15rpx 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-list {
    .floor-title {
      width: 100%;
      height: 60rpx;
    }

    .floor-item-box {
      display: flex;
      padding-left: 10rpx;

      .right-img-box {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }
  }
</style>
